#include "control_base.hpp"
#include <imgui.h>
#include "content/data_provider.hpp"
#include "imgui_ext/imgui_ext.h"
#include "text/IconsFontAwesome6.h"

void ui_control::bind (bindable_base *data)
{
    if (caption_)
        caption_->attach (data);
    if (helper_)
        helper_->attach (data);
    update_data (data);
}

void ui_control::init_content (const xml_node &node)
{
    std::string caption_text = node.attribute ("caption").as_string ();
    std::string help_text    = node.attribute ("tooltip").as_string ();
    if (!caption_text.empty ())
    {
        caption_ = create_data (caption_text, data_category::std_string);
    }
    if (!help_text.empty ())
    {
        helper_ = create_data (help_text, data_category::std_string);
    }
    else if (auto tn = node.child ("tooltip"))
    {
    }
}

void ui_control::before_draw ()
{
    if (caption_)
    {
        auto caption = caption_->get_std_string ();
        if (!caption.empty ())
        {
            ImGui::Text ("%s", caption.c_str ());
            ImGui::SameLine ();
        }
    }
}

void ui_control::after_draw ()
{
    if (helper_)
    {
        switch (helper_->type ())
        {
        case data_category::c_string:
        case data_category::std_string: {
            auto text = helper_->get_std_string ();
            ImGui::TextHelperSign ("%s", text.c_str ());
            break;
        }
        case data_category::ui: {
            auto ui = helper_->get_ui ();
            ImGui::SameLine ();
            ImGui::TextUnformatted (ICON_FA_CIRCLE_QUESTION);
            if (ImGui::IsItemHovered ())
            {
                ImGui::BeginTooltip ();
                ui->draw ();
                ImGui::EndTooltip ();
            }
            break;
        }
        default: break;
        }
    }
}

void ui_control::draw_children ()
{
    // no children
}
